<?

$action = new Action();
class Action
{
	function Action()
	{
		global $gAct,$gOp;
		switch($gOp)
		{
			case "queryKey":
			    $this->queryKey();
				break;
			case "getLstFile":
			    $this->getLstFile();
			break;
			case "getLstFileFront":
			    $this->getLstFileFront();
			break;
            case "getNavData":
			    $this->getNavData();
			break;
			case "addFav":
			    $this->addFav();
			break;
			case "getFileInfo":
			$this->getFileInfo();
			break;
			case "getRefer":
			$this->getRefer();
			break;
			case "getFileEditInfo":
			$this->getFileEditInfo();
			break;
			case "sendAppEmail":
			$this->sendAppEmail();
			break;
			case "checkPost":
			$this->checkPost();
			break;
			case "getCheckUserEmail":
			$this->getCheckUserEmail();
			break;
			case "getAppLstFile":
			$this->getAppLstFile();
			break;
		}
	}
function getCheckUserEmail()
{
	global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB,$gCurTime,$gFileType;
	$DB->db_query("select * from admin_info where roleId=4");
	while($r=$DB->db_fetch_row())
	{
		$data[]= array("id"=>$r['id'],"realName"=>$r['realName'],"email"=>$r['email']);
	}
	echo $GJson->encode(array("st"=>"ok","data"=>$data));
}
function getFileEditInfo()
	{
		global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB,$gCurTime,$gFileType;
		if($_SESSION['adminUser']['roleId']==3)
		{
		    exit;	 
		}
		if(empty($INCOME['id']) && $INCOME['sid'])
		{
			$INCOME['id'] = $INCOME['sid'];
			$INCOME['isSub'] = 1;
		}
		if($INCOME['isSub']==1)
		{
			$rf = $DB->db_query_first("select * from res_file_subject where id=".$INCOME['id']." and isDel=0");
		}
        else
        {
 			if(strpos($INCOME['id'],",")!==false)
        	{
        		$rf = $DB->db_query_first("select * from res_file_subject where id=".$INCOME['sid']." and isDel=0");
        	}
        	else
        	{
        		$rf = $DB->db_query_first("select * from res_file where id=".$INCOME['id']." and isDel=0");
        	}
        }
		//$rf['path'] = str_replace("s.",".",$rf['path']);
		$rf['size'] = $this->sizecount($rf['size']);
	
		 
 		$fileType = $gFileType[$rf['ext']];
		//var_dump($fileType);
		$rf['isPic'] = 1;
		//echo $fileType['id'];
		if($fileType['id']!=39) //非图片
		{
			//$rf['path'] = str_replace(".".$rf['ext'],".jpg",$rf['path']);
			$rf['isPic'] = 0;
			$rf['path'] = str_replace(".","sb.",$rf['pic']);
		}
		//echo $rf['path'];
		//echo $rf['path'];exit;
		if(!file_exists($rf['path']))
		{
			$rf['path'] = $fileType['p'];
		}
		if(empty($rf['avaliDate']) || $rf['avaliDate']=="")
		{
			$rf['avaliDate'] = date("Y-m-d",strtotime("+1 year"));
		}
		//if(empty($rf['zhuanTi']))
		//{
		//	$sql = "select zhuanTi from res_file where uid=".$_SESSION['adminUser']['id']." and zhuanTi !='' and zhuanTi is not null order by id desc";
		//	$zhuanTir = $DB->db_query_first($sql);
		//	$rf['zhuanTi'] = $zhuanTir['zhuanTi'];
		//}
		echo $GJson->encode(array("st"=>"ok","data"=>$rf));
	}
	function getRefer()
	{
		global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB,$gCurTime;
		$sql = "SELECT * FROM res_ref order by id";
		$DB->db_query($sql);
		 
		$data = null;
		$idx = 0+($page-1)*$iPerPage;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
            if(empty($r['pic']))
			{
				$r['pic'] = 'res/de.png';
			}
			$data[] = $r;
		}
		if(empty($data))
		{
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
			$data[]['pic'] = 'res/de.png';
		}
		
	    echo json_encode($data);
	}
	function queryKey()
	{
		global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB,$gCurTime;
	   $k = array();
	   if($INCOME['k'])
	   {
		 $DB->db_query("select mark from res_file where mark like '".$INCOME['k']."%' and isDel=0 group by mark");
		
		while($r=$DB->db_fetch_row())
		{
			$k[] = $r['mark'];
		}
		
	   }
	   echo $GJson->encode($k);
	}
	function sizecount($filesize) {
 if($filesize >= 1073741824) {
  $filesize = round($filesize / 1073741824 * 100) / 100 . ' GB';
 } elseif($filesize >= 1048576) {
  $filesize = round($filesize / 1048576 * 100) / 100 . ' MB';
 } elseif($filesize >= 1024) {
  $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
 } else {
  $filesize = $filesize.' bytes';
 }
 return $filesize;
}
	function getFileInfo()
	{
		global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB,$gCurTime,$gFileType;
		$rf = $DB->db_query_first("select year,pic,id,author,mark,path,ext,size,uname,zhuanTi,instro,limitUse,avaliDate,keywords,fileTypeName,isPost from res_file where id=".$INCOME['id']." and isDel=0");
		//$rf['path'] = str_replace("s.",".",$rf['path']);
		$rf['size'] = $this->sizecount($rf['size']);
	
		$keys =explode(" ",$rf['keywords']);
 		$fileType = $gFileType[$rf['ext']];
 
		if($fileType['id']==39)
		{
			$rf['path'] = str_replace(".","b.",$rf['path']);
		}
		else
		{
			if(empty($rf['pic'])|| $rf['pic']=="")
			{
				$rf['pic'] = $fileType['p'];
				if(empty($rf['pic']))
				{
						$rf['pic'] ="images/ico/doc.png";
				}
			}
			else
			{
				if(strpos($rf['pic'],"sb.")===false)
				{
					$rf['pic'] = str_replace(".","sb.",$rf['pic']);
				}
			}
			$rf['path'] = $rf['pic'];
		}
       
		if(!file_exists($rf['path']))
		{
			$rf['path'] = $fileType['p'];
		}
			 
		if($keys && count($keys)>0)
		{
			$rf['keywords'] = "";
			foreach($keys as $v)
			{
				$rf['keywords'] = $rf['keywords']."&nbsp;<a href='?act=main&k=".rawurlencode($v)."'>".$v."</a>";
			}
		}
		else
		{
			$rf['keywords'] = "<a href='?act=main&k=".rawurlencode($rf['keywords'])."'>".$rf['keywords'] ."</a>";
		}
		echo $GJson->encode(array("st"=>"ok","data"=>$rf));
	}
	function addFav()
	{
		global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB,$gCurTime;
		$f['addTime'] = $gCurTime;
		$f['fid'] = $INCOME['id'];
		$f['uid'] = $_SESSION['adminUser']['id'];
		$f['uname'] = $_SESSION['adminUser']['name'];
		$r = $DB->db_query_first("select * from res_fav where uid=".$f['uid']." and fid=".$f['fid']);
		if(empty($r))
		{
			$DB->db_form_insert("res_fav",$f);
		}
		echo "ok";
	}
	function getNavData()
	{
		global $gSmarty,$gAct,$INCOME,$GJson,$_SESSION,$DB;
 
		if($_SESSION['adminUser']['roleId']==3) //下载
		{
			$sql = "SELECT * FROM type where parId=".$INCOME['id'];
			$DB->db_query($sql);
			$rs = array();
			while($r=$DB->db_fetch_row())
			{
				$rs[] = array("id"=>$r['id'],"name"=>$r['name']);
			}
			echo $GJson->encode(array("name"=>$_SESSION['adminUser']['name'],"menu"=>$rs));
		}
		else
		{
			$sql = "SELECT * FROM `type` where parId=0 and isHide=0";
			$DB->db_query($sql);
			while($r=$DB->db_fetch_row())
			{
				$rs[] = array("id"=>$r['id'],"name"=>$r['name']);
			}
			if($_SESSION['adminUser']['roleId']==2) //上传
			{
				$sql = "select sid from res_file where isPost=4 and uid=".$_SESSION['adminUser']['id']." and isDel=0";
				$DB->db_query($sql);
				$sids = array();
				while($r= $DB->db_fetch_row())
				{
					$sids[]= $r['sid'];
				}
	 
				$r['num'] = count(array_unique($sids));
			}
            else  //管理员/审批
            {
				$sql = "select sid from res_app_file where touid=".$_SESSION['adminUser']['id']." and isChecked=0 and sid!=0";
				$DB->db_query($sql);
				$sids = array();
				while($r= $DB->db_fetch_row())
				{
					$sids[]= $r['sid'];
				}
				$r['num'] = count(array_unique($sids));
            }

			echo $GJson->encode(array("name"=>$_SESSION['adminUser']['name'],"appnum"=>$r['num'],"menu"=>$rs,"rid"=>$_SESSION['adminUser']['roleId']));
		}
	}
	function getLstFile()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$GJson,$gFileType,$gPageName;
		$table = "res_file";
		$timeField = "postTime";
		if($INCOME['b'])
		{
			$conArr[] ="(carIds like '%,".$INCOME['b'].":%'"." or "."brandId = '".$INCOME['b']."')";
		}
		if($INCOME['c'])
		{
			$conArr[] ="(carIds like '%:".$INCOME['c'].",%'"." or "."carId = '".$INCOME['c']."')";
		}
		if($INCOME['t'])
		{
			$conArr[] ="(typeIds like '%,".$INCOME['t'].":%'"." or "."typeId = '".$INCOME['t']."')";
		}
		if($INCOME['s'])
		{
			$conArr[] ="(typeIds like '%:".$INCOME['s'].",%'"." or "."styleId = '".$INCOME['s']."')";
		}
		if($INCOME['y'])
		{
			$conArr[] ="year = '".$INCOME['y']."'";
		}
		//if($INCOME['gid'])
		//{
		//	$firs = $DB->db_query_first("select zhuanTi from res_file where id='".$INCOME['gid']."' and isDel=0");
		//	$conArr[] ="zhuanTi = '".$firs['zhuanTi']."' and isGroupTop=0";
		//}
		if($INCOME['gid'])
		{
			//$groupTop = $DB->db_query_first("select * from res_file where id=".$INCOME['gid']." and isDel=0");
			//if($groupTop)
			//{
			//	$conArr[] ="zhuanTi = '".$groupTop['zhuanTi']."' and isGroupTop=0";
			//}
			$conArr[] ="sid = '".$INCOME['gid']."'";
		}
		else
		{
			
			if($INCOME['f'])
			{
				$INCOME['f'] = 42;
				if($INCOME['f']== "42")
				{
					//$conArr[] ="isGroupTop !=0";
					$table='res_file_subject';
				}
				else
				{
					$conArr[] ="fileTypeId = '".$INCOME['f']."'";
					$conArr[] ="isGroupTop =0";
				}
			}
		}

		if($INCOME['did'])
		{
			$conArr[] ="dirIds like '%,".$INCOME['did'].",%'";
		}

		//if($INCOME['f'])
		//{
			//$conArr[] ="ext = '".$INCOME['did']."'";
		//}
		if($INCOME['k'])
		{
			//if($INCOME['k']==-1)
			//{
			//	$conArr[] ="isPost = 0";
			//}
			//else if($INCOME['k']==1)
			//{
			//	$conArr[] ="isPost = 1";
			//}
			//else 
			if($INCOME['k']=="myFav")
			{
				$sql="SELECT fid FROM res_fav  where uid=".$_SESSION['adminUser']["id"];
				$DB->db_query($sql);
				$r = $DB->db_fetch_array();
				$vx = array();
				foreach($r as $v)
				{
					$vx[]=$v['fid'];
				}
				if(is_array($vx) && count($vx)>0)
				{
					$conArr[] ="id in(".implode(",",$vx).")";
				}
				else
				{
					$conArr[] ="id =0";
				}
			}
			else if($INCOME['k']=="newPic")
			{
				$conArr[] ="ext in('jpg','png')";
			}
			else if($INCOME['k']=="newVideo")
			{
				$conArr[] ="ext in('mp4')";
			}
			else
			{
				$conArr[] ="(mark like '%".$INCOME['k']."%' or name like '%".$INCOME['k']."%' or keywords like '%".$INCOME['k']."%')";
			}
		}
		if($_SESSION['adminUser']['roleId']==3)
		{
			$conArr[] ="isPost = 1";
		}
		else
		{
			if($INCOME['src']=="edit")  //后台获取数据
			{
				if($INCOME['st'])
				{
					if($INCOME['st']==5)
					{
						if(($_SESSION['adminUser']['roleId']==4 || $_SESSION['adminUser']['roleId']==1) && $INCOME['sid']==null)
						{
							if($INCOME['cbid'])
							{
								$concbid = " and batchId='".$INCOME['cbid']."'";
							}
							$sql = "select sid from res_app_file where touid=".$_SESSION['adminUser']['id']." and isChecked=0 ".$concbid." group by sid";
							//echo $sql;
							$DB->db_query($sql);
							//$conArr[] ="isPost in (3,4,5)";
							$allId = array();
							while($r=$DB->db_fetch_row())
							{
								$allId[] = $r['sid'];
							}
							if(empty($INCOME['gid']))
							{
								$table = "res_file_subject";
								if(is_array($allId) && count($allId)>0)
								{
									$conArr[] ="id in(".implode(",",$allId).")";
								}
								if(count($allId)==0)
								{
									$conArr[] ="id in(0)";
								}
							//echo count($allId);
							}
							//echo "aaaaa";
						}
						
						//var_dump($conArr);
					    //var_dump($conArr);
					}
					else  if($INCOME['st']==4)
					{
						if(empty($INCOME['gid']))
						{
							$table = "res_file_subject";
						}
							
					}
					else
					{
						$conArr[] ="isGroupTop = 0"; 
						
					}
					if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
					{
							$conArr[] ="uid = ".$_SESSION['adminUser']['id'];
					}
					$conArr[] ="isPost = ".$INCOME['st'];
				}
				else
				{
					$conArr[] ="isPost = 0";
					if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
					{
						$conArr[] ="uid = ".$_SESSION['adminUser']['id'];
					}
				}
				
				$timeField = "addTime";
				if($INCOME['sid']!=null && $INCOME['st']!=4)
				{
					
					if($INCOME['sid']==-1 || $INCOME['sid'] =="0")
					{
						$INCOME['sid'] = 0;
					}
					$conArr[] ="sid = ".$INCOME['sid'];
				}
                else
                {
                	if($INCOME['ajax']!="getAppListPage")
                	{
                		$table="res_file";
						if($INCOME['sid']==-1 || $INCOME['sid'] =="0")
						{
							$INCOME['sid'] = 0;
						}
						$conArr[] ="sid = ".$INCOME['sid'];
                	}
                	
                }
				//svar_dump($conArr);
			}
			else {
				$conArr[] ="isPost = 1";
			}

		}
		if($conArr)
		{
			$con ="where ". implode(" and ",$conArr)." and isDel=0";
		}
        
 		$sql = "SELECT count(*) as num FROM $table $con";
 		//echo $sql;
		$iPerPage = 12;
		if($INCOME['src']=="edit")
		{
		    $iPerPage = 18;
		}
		if($INCOME['src']=="m")
		{
		    $iPerPage = 9;
		}
		if($INCOME['ps'])
		{
			$iPerPage = $INCOME['ps'];
		}
		if($iPerPage>50)
		{
			$iPerPage = 1;
		}
		$r = $DB->db_query_first($sql);
		$allNum = $r['num'];
		$totalPage = ceil($allNum/$iPerPage);
		$ajax = $INCOME['ajax']?$INCOME['ajax']:"getPage";
		$queryUrl = $gPageName."?act=".$gAct."&op=".$gOp."&f=".$INCOME['f']."&b=".$INCOME['b']."&c=".$INCOME['c']."&t=".$INCOME['t']."&s=".$INCOME['s']."&y=".$INCOME['y']."&k=".$_GET['k']."&did=".$INCOME['did']."&ajax=".$ajax."&st=".$INCOME['st']."&ps=".$INCOME['ps']."&gid=".$INCOME['gid']."&src=".$INCOME['src'];
		$pageClass = new MyPage(array (
		'total' => $allNum,
		'perpage' => $iPerPage,
		'url' => $queryUrl
		));
		$pageClass->open_Ajax($ajax);
		$page = empty ($INCOME['page']) ? 1 : $INCOME['page'];
		$iStartNo = ($page -1) * $iPerPage;
		$sql = "SELECT id,name,path,ext,mark,fileTypeName,isGroupTop,pic FROM $table $con order by $timeField desc LIMIT {$iStartNo}, {$iPerPage}";
		//echo $sql;
		$DB->db_query($sql);
		$data = null;
		$idx = 0+($page-1)*$iPerPage;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
			//$r['disId'] = $idx;
			$fileType = $gFileType[$r['ext']];
			if($fileType['n']=="图片")
			{
				$r['pic'] = $r['path'];
			}
			else
			{
				if(empty($r['pic'])|| $r['pic']=="")
				{
					$r['pic'] = $fileType['p'];
					if(empty($r['pic']))
					{
						$r['pic'] ="images/ico/doc.png";
					}
				}
				else
				{
					if(strpos($r['pic'],"s.")===false)
					{
						$r['pic'] = str_replace(".","s.",$r['pic']);
					}
				}

			}
			if($r['mark'])
			{
				$r['name'] = $r['mark']."<br/>".$r['name'];
			}
		    $data[] = $r;
			unset($fileType);	
		}
		$pageNav = $pageClass->show();
		if($INCOME['src']=="m")
		{
			$pageNav = $pageClass->simpleShow();
			$pageNav = str_replace("上一页","<img src='images/p.png'/>",$pageNav);
			$pageNav = str_replace("下一页","<img src='images/n.png'/>",$pageNav);
			$pageNav = str_replace("span","div",$pageNav);
		}
		echo $GJson->encode(array("st"=>"ok","tp"=>$totalPage,"u"=> $queryUrl,"data"=>array("data"=>$data,"total"=>$allNum,"pageNav"=>$pageNav)));
		 
	}

function sendAppEmail()
{
	global $GJson,$INCOME,$DB,$_SESSION,$gWeb,$CONF,$gCurTime;
	$id = $INCOME['id'];
	if($id)
	{
		$subjectIds = array();
		$resIds = array();
		$sid = $INCOME['sid'];
		$appCon = "uid=".$_SESSION['adminUser']['id']." and isDel=0";
		if($id=="-1" && $sid=="0") //全部主题 及 全部主题下的文件
		{
			$appCon = $appCon." and isPost=4";
		}
		else if($id=="-1" && $sid!="0") //某个主题下的 全部文件
		{
			$appCon = $appCon." and isPost=4  and sid in(".$sid.")";
		}
		else if($id!="-1" && $sid=="0") //某个主题
		{
			$appCon = $appCon." and isPost=4  and sid in(".$id.")";
			
		}
		else if($id!="-1" && $sid!="0") //某个主题下的某个文件
		{
			$appCon = $appCon." and isPost=4  and id in(".$id.")"; 
		}
 		$DB->db_query("select id,sid,dirIds from res_file where ".$appCon);
		$resIds = array();
		while($r = $DB->db_fetch_row())
		{
			$resIds[] = array("id" =>$r['id'],"sid"=>$r['sid'],"dirIds"=>$r['dirIds']);
		}
		$DB->db_query("update res_file set isPost = 5 where ".$appCon);
		$toR = $DB->db_query_first("select id,email,pass from admin_info where id=".$INCOME['cid']." and roleId=4");
		//$body = "内容厨房内容审批" ;
		if($toR && $toR['email']!="")
		{
		$ff['uid']= $_SESSION['adminUser']['id'];
		$ff['uname']= $_SESSION['adminUser']['name'];
		$ff['touid']= $INCOME['cid'];
		$ff['touname']= $toR['name'];
		$ff['addTime']= $gCurTime;
		$ff['batchId'] = uniqid();
		foreach($resIds as $v)
		{
			$ff['fid']= $v['id'];
			$ff['sid']= $v['sid'];
			$ff['dirIds']= $v['dirIds'];
			$rssss = $DB->db_query_first("select * from res_app_file where sid=".$v['sid']." and fid=".$v['id']);
			if(empty($rssss))
			{
				$DB->db_form_insert("res_app_file",$ff);
			}
		}
		 
    	$email = $toR['email'];
    	$param[] = $toR['id'];
		$param[] = trim($toR['pass']);
		$param[] = trim($ff['batchId']);
		//$txt = serialize($param);
		$txt = implode(",,",$param);
	    $encrypt = $gWeb->passportEncrypt($txt,$CONF['dbkey']); 
	    $url = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF']."?act=login&op=doLogin&src=email&k=".rawurlencode($encrypt);
    	//$url = "http://localhost/jeep/resman/?act=login&op=doLogin";
		$body = "申请人：".$_SESSION['adminUser']['name']."，申请发布内容".count($resIds)."个，请确认<br/>";
		$isSuccess = $this->smtp_mail($email, "内容厨房 发布申请",$body.$url, "yourdomain.com", "username"); 
		echo $GJson->encode(array("st"=>"ok","send"=>$isSuccess));
		}
        else
        {
        echo $GJson->encode(array("st"=>"err1","send"=>false));
        }
	}
	else
	{
		echo $GJson->encode(array("st"=>"err","send"=>false));
	}
}
 function smtp_mail($sendto_email,$subject,$body,$extra_hdrs,$user_name){   
 	    include "lib/mail/class.phpmailer.php";
        $mail = new PHPMailer();   
        $mail->IsSMTP();                  // send via SMTP   
        $mail->Host = "smtp.sina.net";   // SMTP servers   
        $mail->SMTPAuth = true;           // turn on SMTP authentication   
        $mail->Username = "shsjxt@a4a.com.cn";     // SMTP username  注意：普通邮件认证不需要加 @域名   
        $mail->Password = "a4a2015it"; // SMTP password   
        $mail->From = "shsjxt@a4a.com.cn";      // 发件人邮箱   
        $mail->FromName =  "内容厨房系统";  // 发件人   

        $mail->CharSet = "UTF-8";   // 这里指定字符集！   
        $mail->Encoding = "base64";   
        $mail->AddAddress($sendto_email,"username");  // 收件人邮箱和姓名   
        //$mail->AddReplyTo("yourmail@yourdomain.com","yourdomain.com");   
        //$mail->WordWrap = 50; // set word wrap 换行字数   
        //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件   
        //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");   
        $mail->IsHTML(true);  // send as HTML   
        // 邮件主题   
        $mail->Subject = $subject;   
        // 邮件内容   
        $mail->Body = $body;                                               
        $mail->AltBody ="text/html";   
        if(!$mail->Send())   
        {   
            //echo "邮件发送有误 <p>";   
            //echo "邮件错误信息: " . $mail->ErrorInfo;   
            return false;
            //exit;   
        }   
        else {   
            //echo "$user_name 邮件发送成功!<br />";   
            return true;
        }   
  } 
  function checkPost()
  {
	global $GJson,$INCOME,$DB,$_SESSION,$gWeb,$CONF,$gCurTime;
	if($_SESSION['adminUser']['roleId']=="1" || $_SESSION['adminUser']['roleId']=="4")
	{
	$id = $INCOME['id'];
	$bid = $INCOME['bid'];
	$sid = $INCOME['sid'];
	if($id || $bid)
	{
		$appCon = "touid=".$_SESSION['adminUser']['id'];
		if($bid)
		{
			$appCon = $appCon." and batchId='".$bid."'";
		}
		$subjectId = array();
		if($id=="-1" && $sid=="0") //全部主题 全部文件
		{
 
		}
		else if($id=="-1" && $sid!="0") //某个主题下的 全部文件
		{
			$appCon = $appCon." and sid= ".$sid;
		}
		else if($id!="-1" && $sid=="0") //某个主题
		{
			$appCon = $appCon." and sid= ".$id;
		}
		else if($id!="-1" && $sid!="0") //某个主题下的某个文件
		{
			$appCon = $appCon." and sid= ".$sid." and fid in(".$id.")";
		}
		//echo "select fid,sid from res_app_file where $appCon";
		$DB->db_query("select fid,sid from res_app_file where $appCon");
		while($r=$DB->db_fetch_row())
		{
		    $idarr[] = $r['fid'];
		    $subjectId[] = $r['sid'];
		}
		$id = implode(",",$idarr);
		$DB->db_query("update res_file set isPost = 1,postTime='".$gCurTime."' where isPost=5 and id in(".$id.")");
		$DB->db_query("update res_app_file set isChecked = 1,checkTime='".$gCurTime."' where $appCon");
		$subjectId = array_unique($subjectId);
		if(count($subjectId)>0)
		{
			$DB->db_query("update res_file_subject set isPost=1,postTime='".$gCurTime."' where id in(".implode(",",$subjectId).")");
		}
		//$fileInfo = $DB->db_query_first("select zhuanTi from res_file where id in(".$id.") and isGroupTop>0");
		//if($fileInfo)
		//{
		//	$DB->db_query("update res_file set isPost=1 where zhuanTi='".$fileInfo['zhuanTi']."' and isGroupTop=0 and isPost=5");
		//}
		echo $GJson->encode(array("st"=>"ok"));
	}
	else
	{
		echo $GJson->encode(array("st"=>"err"));
	}
	}
	else
	{
	   echo "无权限";
	}
  }
	function getLstFileFront()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$GJson,$gFileType,$gPageName;
		$table = "res_file";
		$timeField = "postTime";

 
		if($INCOME['gid'])
		{
			$conArr[] ="sid = '".$INCOME['gid']."'";
			$conArr[] ="isGroupTop =0";
			$timeField = "addTime";
		}
		else
		{
 			if($INCOME['b'])
			{
				$conArr[] ="(carIds like '%,".$INCOME['b'].":%'"." or "."brandId = '".$INCOME['b']."')";
			}
			if($INCOME['c'])
			{
				$conArr[] ="(carIds like '%:".$INCOME['c'].",%'"." or "."carId = '".$INCOME['c']."')";
			}
			if($INCOME['t'])
			{
				$conArr[] ="(typeIds like '%,".$INCOME['t'].":%'"." or "."typeId = '".$INCOME['t']."')";
			}
			if($INCOME['s'])
			{
				$conArr[] ="(typeIds like '%:".$INCOME['s'].",%'"." or "."styleId = '".$INCOME['s']."')";
			}
			if($INCOME['y'])
			{
				$conArr[] ="year = '".$INCOME['y']."'";
			}
			if($INCOME['f'])
			{
				$INCOME['f'] = 42;
				if($INCOME['f']== "42")
				{
					//$conArr[] ="isGroupTop !=0";
					$table='res_file_subject';
				}
				else
				{
					$conArr[] ="fileTypeId = '".$INCOME['f']."'";
					$conArr[] ="isGroupTop =0";
				}
			}
		}

		if($INCOME['did'])
		{
			$conArr[] ="dirIds like '%,".$INCOME['did'].",%'";
		}
 
		if($INCOME['k'])
		{
 
			if($INCOME['k']=="myFav")
			{
				$sql="SELECT fid FROM res_fav  where uid=".$_SESSION['adminUser']["id"];
				$DB->db_query($sql);
				$r = $DB->db_fetch_array();


				$vx = array();
				foreach($r as $v)
				{
					$vx[]=$v['fid'];
				}
				if(is_array($vx) && count($vx)>0)
				{
					$conArr[] ="id in(".implode(",",$vx).")";
				}
				else
				{
					$conArr[] ="id =0";
				}
			}
			else if($INCOME['k']=="newPic")
			{
				$conArr[] ="ext in('jpg','png')";
			}
			else if($INCOME['k']=="newVideo")
			{
				$conArr[] ="ext in('mp4')";
			}
			else
			{
				$conArr[] ="(mark like '%".$INCOME['k']."%' or name like '%".$INCOME['k']."%' or keywords like '%".$INCOME['k']."%')";
			}
		}
		//名称
		if($INCOME['name'])
		{
			//$conArr[] ="name = '".$INCOME['name']."'";

		}
		$conArr[] ="isPost = 1";
		if($conArr)
		{
			if($INCOME['name'])
			{
				$con ="where ". implode(" and ",$conArr)." and isDel=0";
			}
			$con ="where ". implode(" and ",$conArr)." and isDel=0";
		}
        
 		$sql = "SELECT count(*) as num FROM $table $con";
 		//echo $sql;
		$iPerPage = 12;
		if($INCOME['src']=="edit")
		{
		    $iPerPage = 18;
		}
		if($INCOME['src']=="m")
		{
		    $iPerPage = 9;
		}
		if($INCOME['ps'])
		{
			$iPerPage = $INCOME['ps'];
		}
		if($iPerPage>50)
		{
			$iPerPage = 1;
		}
		$r = $DB->db_query_first($sql);
		$allNum = $r['num'];
		$totalPage = ceil($allNum/$iPerPage);
		$ajax = $INCOME['ajax']?$INCOME['ajax']:"getPage";
		if($INCOME['name']=='name')
		{
			//$conArr[] ="name = '".$INCOME['name']."'";
			$queryUrl = $gPageName."?act=".$gAct."&op=".$gOp."&f=".$INCOME['f']."&b=".$INCOME['b']."&c=".$INCOME['c']."&t=".$INCOME['t']."&s=".$INCOME['s']."&y=".$INCOME['y']."&k=".$_GET['k']."&did=".$INCOME['did']."&ajax=".$ajax."&st=".$INCOME['st']."&ps=".$INCOME['ps']."&gid=".$INCOME['gid']."&src=".$INCOME['src']."&name=".$INCOME['name'];

		}else{
			$queryUrl = $gPageName."?act=".$gAct."&op=".$gOp."&f=".$INCOME['f']."&b=".$INCOME['b']."&c=".$INCOME['c']."&t=".$INCOME['t']."&s=".$INCOME['s']."&y=".$INCOME['y']."&k=".$_GET['k']."&did=".$INCOME['did']."&ajax=".$ajax."&st=".$INCOME['st']."&ps=".$INCOME['ps']."&gid=".$INCOME['gid']."&src=".$INCOME['src'];
		}
		
		$pageClass = new MyPage(array (
		'total' => $allNum,
		'perpage' => $iPerPage,
		'url' => $queryUrl
		));
		$pageClass->open_Ajax($ajax);
		$page = empty ($INCOME['page']) ? 1 : $INCOME['page'];
		$iStartNo = ($page -1) * $iPerPage;
		if($INCOME['name']=='name')
		{
			
			$sql = "SELECT id,name,path,ext,mark,fileTypeName,isGroupTop,pic,uid,addTime FROM $table $con order by name asc LIMIT {$iStartNo}, {$iPerPage}";

		}else{
			$sql = "SELECT id,name,path,ext,mark,fileTypeName,isGroupTop,pic,uid,addTime FROM $table $con order by id desc LIMIT {$iStartNo}, {$iPerPage}";
		}
		
		//echo $sql;
		$DB->db_query($sql);
		$data = null;
		$idx = 0+($page-1)*$iPerPage;
		
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
			//$r['disId'] = $idx;
			$fileType = $gFileType[$r['ext']];
			if($fileType['n']=="图片")
			{
				$r['pic'] = $r['path'];
			}
			else
			{
				if(empty($r['pic'])|| $r['pic']=="")
				{
					$r['pic'] = $fileType['p'];
					if(empty($r['pic']))
					{
						$r['pic'] ="images/ico/doc.png";
					}
				}
				else
				{
					if(strpos($r['pic'],"s.")===false)
					{
						$r['pic'] = str_replace(".","s.",$r['pic']);
					}
				}

			}
			if($r['mark'])
			{
				$r['name'] = $r['mark']."<br/>".$r['name'];
			}
			if($table=="res_file")
			{
				$r['s'] = 0;
			}
			else
			{
				$r['s'] = 1;
			}
			$r['e'] = 0;
			if($_SESSION['adminUser']['roleId']==1 ||$_SESSION['adminUser']['roleId']==4 || $_SESSION['adminUser']['id']==$r['uid'])
			{
			    $r['e'] = 1;
			}
		    $data[] = $r;
			unset($fileType);	
		}
		$pageNav = $pageClass->show();
		if($INCOME['src']=="m")
		{
			$pageNav = $pageClass->simpleShow();
			$pageNav = str_replace("上一页","<img src='images/p.png'/>",$pageNav);
			$pageNav = str_replace("下一页","<img src='images/n.png'/>",$pageNav);
			$pageNav = str_replace("span","div",$pageNav);
		}
		echo $GJson->encode(array("st"=>"ok","tp"=>$totalPage,"u"=> $queryUrl,"data"=>array("data"=>$data,"total"=>$allNum,"pageNav"=>$pageNav)));
		 
	}
	
	function getAppLstFile()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$_SESSION,$GJson,$gFileType,$gPageName;
		$table = "res_file";
		$timeField = "postTime";
		if($INCOME['b'])
		{
			$conArr[] ="(carIds like '%,".$INCOME['b'].":%'"." or "."brandId = '".$INCOME['b']."')";
		}
		if($INCOME['c'])
		{
			$conArr[] ="(carIds like '%:".$INCOME['c'].",%'"." or "."carId = '".$INCOME['c']."')";
		}
		if($INCOME['t'])
		{
			$conArr[] ="(typeIds like '%,".$INCOME['t'].":%'"." or "."typeId = '".$INCOME['t']."')";
		}
		if($INCOME['s'])
		{
			$conArr[] ="(typeIds like '%:".$INCOME['s'].",%'"." or "."styleId = '".$INCOME['s']."')";
		}
		if($INCOME['y'])
		{
			$conArr[] ="year = '".$INCOME['y']."'";
		}
		//if($INCOME['gid'])
		//{
		//	$firs = $DB->db_query_first("select zhuanTi from res_file where id='".$INCOME['gid']."' and isDel=0");
		//	$conArr[] ="zhuanTi = '".$firs['zhuanTi']."' and isGroupTop=0";
		//}
		if($INCOME['gid'])
		{
			//$groupTop = $DB->db_query_first("select * from res_file where id=".$INCOME['gid']." and isDel=0");
			//if($groupTop)
			//{
			//	$conArr[] ="zhuanTi = '".$groupTop['zhuanTi']."' and isGroupTop=0";
			//}
			$conArr[] ="sid = '".$INCOME['gid']."'";
		}
		else
		{
			
			if($INCOME['f'])
			{
				$INCOME['f'] = 42;
				if($INCOME['f']== "42")
				{
					//$conArr[] ="isGroupTop !=0";
					$table='res_file_subject';
				}
				else
				{
					$conArr[] ="fileTypeId = '".$INCOME['f']."'";
					$conArr[] ="isGroupTop =0";
				}
			}
		}

		if($INCOME['did'])
		{
			$conArr[] ="dirIds like '%,".$INCOME['did'].",%'";
		}

		//if($INCOME['f'])
		//{
			//$conArr[] ="ext = '".$INCOME['did']."'";
		//}
		if($INCOME['k'])
		{
			//if($INCOME['k']==-1)
			//{
			//	$conArr[] ="isPost = 0";
			//}
			//else if($INCOME['k']==1)
			//{
			//	$conArr[] ="isPost = 1";
			//}
			//else 
			if($INCOME['k']=="myFav")
			{
				$sql="SELECT fid FROM res_fav  where uid=".$_SESSION['adminUser']["id"];
				$DB->db_query($sql);
				$r = $DB->db_fetch_array();
				$vx = array();
				foreach($r as $v)
				{
					$vx[]=$v['fid'];
				}
				if(is_array($vx) && count($vx)>0)
				{
					$conArr[] ="id in(".implode(",",$vx).")";
				}
				else
				{
					$conArr[] ="id =0";
				}
			}
			else if($INCOME['k']=="newPic")
			{
				$conArr[] ="ext in('jpg','png')";
			}
			else if($INCOME['k']=="newVideo")
			{
				$conArr[] ="ext in('mp4')";
			}
			else
			{
				$conArr[] ="(mark like '%".$INCOME['k']."%' or name like '%".$INCOME['k']."%' or keywords like '%".$INCOME['k']."%')";
			}
		}
		if($_SESSION['adminUser']['roleId']==3)
		{
			$conArr[] ="isPost = 1";
		}
		else
		{
			if($INCOME['src']=="edit")  //后台获取数据
			{
				if($INCOME['st'])
				{
					if($INCOME['st']==5)
					{
						if(($_SESSION['adminUser']['roleId']==4 || $_SESSION['adminUser']['roleId']==1) && $INCOME['sid']==null)
						{
							if($INCOME['cbid'])
							{
								$concbid = " and batchId='".$INCOME['cbid']."'";
							}
							if(empty($INCOME['gid']))
							{
								$sql = "select sid from res_app_file where touid=".$_SESSION['adminUser']['id']." and isChecked=0 and sid!=0 ".$concbid." group by sid";
								//echo $sql;
								$DB->db_query($sql);
								//$conArr[] ="isPost in (3,4,5)";
								$allId = array();
								while($r=$DB->db_fetch_row())
								{
									$allId[] = $r['sid'];
								}
								$table = "res_file_subject";
								if(is_array($allId) && count($allId)>0)
								{
									$conArr[] ="id in(".implode(",",$allId).")";
								}
								if(count($allId)==0)
								{
									$conArr[] ="id in(0)";
								}
							//echo count($allId);
							}
							else
							{
								$conArr[] ="isPost = ".$INCOME['st'];
								$sql = "select fid from res_app_file where touid=".$_SESSION['adminUser']['id']." and isChecked=0 ".$concbid;
								//echo $sql;
								$DB->db_query($sql);
								//$conArr[] ="isPost in (3,4,5)";
								$allId = array();
								while($r=$DB->db_fetch_row())
								{
									$allId[] = $r['fid'];
								}
								$conArr[] ="id in(".implode(",",$allId).")";
							}
							//echo "aaaaa";
						}
						//$conArr[] ="isPost = ".$INCOME['st'];
						//var_dump($conArr);
					    //var_dump($conArr);
					}
					else  if($INCOME['st']==4)
					{
						if(empty($INCOME['gid']))
						{
							$table = "res_file_subject";
							$DB->db_query("select sid from res_file where isPost=4 and uid=".$_SESSION['adminUser']['id']." and isDel=0 group by sid");
							if($DB->db_fetch_num()>0)
							{
								$sids = array();
								while($r=$DB->db_fetch_row())
								{
									$sids[] = $r['sid'];
								}
								$conArr[] ="id in(".implode(",",$sids).")";
							}
							else
							{
								$conArr[] ="id in(-1)";
							}
						}
						else
						{
							$conArr[] ="isPost = ".$INCOME['st'];
						}
							
					}
					else
					{
						$conArr[] ="isGroupTop = 0"; 
						$conArr[] ="isPost = ".$INCOME['st'];
						
					}
					if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
					{
							$conArr[] ="uid = ".$_SESSION['adminUser']['id'];
					}
					
				}
				else
				{
					$conArr[] ="isPost = 0";
					if($_SESSION['adminUser']['roleId']!=1 && $_SESSION['adminUser']['roleId']!=4)
					{
						$conArr[] ="uid = ".$_SESSION['adminUser']['id'];
					}
				}
				
				$timeField = "addTime";
				if($INCOME['sid']!=null && $INCOME['st']!=4)
				{
					
					if($INCOME['sid']==-1 || $INCOME['sid'] =="0")
					{
						$INCOME['sid'] = 0;
					}
					$conArr[] ="sid = ".$INCOME['sid'];
				}
                else
                {
                	if($INCOME['ajax']!="getAppListPage")
                	{
                		$table="res_file";
						if($INCOME['sid']==-1 || $INCOME['sid'] =="0")
						{
							$INCOME['sid'] = 0;
						}
						$conArr[] ="sid = ".$INCOME['sid'];
                	}
                	
                }
				//svar_dump($conArr);
			}
			else {
				$conArr[] ="isPost = 1";
			}

		}
		if($conArr)
		{
			$con ="where ". implode(" and ",$conArr)." and isDel=0";
		}
        
 		$sql = "SELECT count(*) as num FROM $table $con";
 	
		$iPerPage = 12;
		if($INCOME['src']=="edit")
		{
		    $iPerPage = 18;
		}
		if($INCOME['src']=="m")
		{
		    $iPerPage = 9;
		}
		if($INCOME['ps'])
		{
			$iPerPage = $INCOME['ps'];
		}
		if($iPerPage>50)
		{
			$iPerPage = 1;
		}
		$r = $DB->db_query_first($sql);
		$allNum = $r['num'];
		$totalPage = ceil($allNum/$iPerPage);
		$ajax = $INCOME['ajax']?$INCOME['ajax']:"getPage";
		$queryUrl = $gPageName."?act=".$gAct."&op=".$gOp."&f=".$INCOME['f']."&b=".$INCOME['b']."&c=".$INCOME['c']."&t=".$INCOME['t']."&s=".$INCOME['s']."&y=".$INCOME['y']."&k=".$_GET['k']."&did=".$INCOME['did']."&ajax=".$ajax."&st=".$INCOME['st']."&ps=".$INCOME['ps']."&gid=".$INCOME['gid']."&src=".$INCOME['src'];
		$pageClass = new MyPage(array (
		'total' => $allNum,
		'perpage' => $iPerPage,
		'url' => $queryUrl
		));
		$pageClass->open_Ajax($ajax);
		$page = empty ($INCOME['page']) ? 1 : $INCOME['page'];
		$iStartNo = ($page -1) * $iPerPage;
		$sql = "SELECT id,name,path,ext,mark,fileTypeName,isGroupTop,pic FROM $table $con order by $timeField desc LIMIT {$iStartNo}, {$iPerPage}";
		//echo $sql;
		$DB->db_query($sql);
		$data = null;
		$idx = 0+($page-1)*$iPerPage;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
			//$r['disId'] = $idx;
			$fileType = $gFileType[$r['ext']];
			if($fileType['n']=="图片")
			{
				$r['pic'] = $r['path'];
			}
			else
			{
				if(empty($r['pic'])|| $r['pic']=="")
				{
					$r['pic'] = $fileType['p'];
					if(empty($r['pic']))
					{
						$r['pic'] ="images/ico/doc.png";
					}
				}
				else
				{
					if(strpos($r['pic'],"s.")===false)
					{
						$r['pic'] = str_replace(".","s.",$r['pic']);
					}
				}

			}
			if($r['mark'])
			{
				//shiwei  审批
				$r['tem_name'] = $r['name'];
				$r['name'] = $r['mark']."<br/>".$r['name'];
				
			}
		    $data[] = $r;
			unset($fileType);	
		}
		$pageNav = $pageClass->show();
		if($INCOME['src']=="m")
		{
			$pageNav = $pageClass->simpleShow();
			$pageNav = str_replace("上一页","<img src='images/p.png'/>",$pageNav);
			$pageNav = str_replace("下一页","<img src='images/n.png'/>",$pageNav);
			$pageNav = str_replace("span","div",$pageNav);
		}
		echo $GJson->encode(array("st"=>"ok","tp"=>$totalPage,"u"=> $queryUrl,"data"=>array("data"=>$data,"total"=>$allNum,"pageNav"=>$pageNav)));
		 
	}
}   
?>